<html>
<title>涨停板</title>
<link type="text/css" rel="stylesheet" href="{{ url_for('static', filename='css/fupan_css.css') }}" />
<script src="{{ url_for('static',filename='js/jquery-2.1.1.min.js') }}"></script>
<script src="{{ url_for('static',filename='js/show_message.js') }}"></script>
<link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
<script src="{{ url_for('static',filename='js/echarts.min.js') }}"></script>
<script src="{{ url_for('static',filename='js/update.js') }}"></script>

<body>
    <div>
        <label for="meeting">选择日期：</label>
        <input id="meeting" type="date" value="2022-01-03" onchange="upperCase(this.id)" />
        <label>覆盖数据</label>
        <input id='exist' type="checkbox" onclick="choice()">
    </div>
    <div class="table-one" style="float:left;margin-right:10px;" align="center">
        <table id="mytable" cellspacing="0"></table>
    </div>
    <div class="table-one" style="float:left;" align="center">
        <table id="broke" cellspacing="0"></table>
    </div>
    <div id="container" style="height:400px;width:30%;float:left;margin-top:60px"></div>
</body>

<!-- 日期事件 -->
<script type="text/javascript">
    exist = false

    function choice() {
        if ($('#exist').is(':checked')) {
            exist = true
        } else {
            exist = false
        }
    }

    function upperCase(x) {
        // 选择日期后停止运行刷新
        if (ID_THREAD != -1) {
            clearInterval(ID_THREAD)
        }

        var date = document.getElementById(x).value
        get_history_limit(date)
        get_history_broke(date)
    }
</script>
<script type="text/javascript">
    var ID_THREAD = -1; //循环线程
    var all_theme = []; //题材数组

    $(document).ready(function() {
        $('#exist').prop('checked', false) //设置复选框不是选中状态
        set_date() //设置日期
        run()
        ID_THREAD = auto_update(run)
    });

    function run() {
        get_now_limit()
        get_broke()
    }



    function get_server_data() {
        var m_data = []
        $.ajax({
            url: '/test',
            data: {},
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                m_data = data;
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    // 获取历史涨停 
    function get_history_limit(date) {
        var m_data = []
        $.ajax({
                url: '/history',
                data: {
                    'date': date //回溯一百天的日期
                },
                type: 'POST',
                async: true,
                dataType: 'json',
                success: function(data) {
                    // m_data = data;
                    data1 = get_history_data(date)
                    set_table_history(data['list'], data1)
                    set_option()
                },
                error: function(msg) {
                    console.log(msg);
                }
            })
            // return m_data;
    };

    // 获取历史开盘收盘数据
    function get_history_data(date) {
        var m_data = []
        $.ajax({
            url: '/history_date',
            data: {
                'data': 100
            },
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                data = data['date']
                dateNext = data[data.indexOf(date) - 1] //下一个交易日
                _search = date + '收盘价 ' + dateNext + '开盘价 ' + dateNext + '收盘价 ' + dateNext + '涨停价 ' + dateNext + '跌停价'
                m_data = search(_search, date, dateNext)
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    //问财搜索
    function search(search, date1, date2) {
        var m_data = []
        $.ajax({
            url: '/searchWencai',
            data: {
                'data': search,
                'exist': exist
            },
            type: 'POST',
            async: false,
            dataType: 'json',
            success: function(data) {
                data = decode_search_data(data, date1, date2)
                m_data = data
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data
    };

    //解析数据
    function decode_search_data(data, date1, date2) {
        data = data['data']
        code = data['columns'].indexOf('code')
        last_close = data['columns'].indexOf('收盘价:不复权[' + date1.replaceAll('-', '') + ']') //昨日收盘价
        _close = data['columns'].indexOf('收盘价:不复权[' + date2.replaceAll('-', '') + ']') //选定日收盘价
        _open = data['columns'].indexOf('开盘价:不复权[' + date2.replaceAll('-', '') + ']') //开盘价
        raising_limit = data['columns'].indexOf('涨停价[' + date2.replaceAll('-', '') + ']') //涨停价
        limit_down = data['columns'].indexOf('跌停价[' + date2.replaceAll('-', '') + ']') //跌停价

        r_data = {}
        for (var i = 0; i < data['data'].length; i++) {
            var _data = []
            _data.push(data['data'][i][last_close])
            _data.push(data['data'][i][_close])
            _data.push(data['data'][i][_open])
            _data.push(data['data'][i][raising_limit])
            _data.push(data['data'][i][limit_down])
            r_data[data['data'][i][code]] = _data
        }
        return r_data
    }

    // 获取历史炸板 
    function get_history_broke(date) {
        var m_data = []
        $.ajax({
                url: '/get_history_broke',
                data: {
                    'date': date
                },
                type: 'POST',
                async: true,
                dataType: 'json',
                success: function(data) {
                    // m_data = data;
                    set_table_broke(data['list'])
                        // set_option()
                },
                error: function(msg) {
                    console.log(msg);
                }
            })
            // return m_data;
    };



    // 设置日期
    function set_date() {
        date = document.getElementById("meeting"); //获取表格的dom节点
        data = get_date()
        date.value = data
    }

    // 获取上个交易日日期
    function get_last_date() {
        var m_data = []
        $.ajax({
            url: '/last_date',
            data: {},
            type: 'POST',
            async: false,
            dataType: 'json',
            // contentType: 'application/json; charset=UTF-8',
            success: function(data) {
                m_data = data;
                console.log(data);
                // update_table(data)
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    function get_time() {
        //获取当前时间
        var d = new Date();
        var m_date = d.getHours() + "-" + d.getMinutes() + "-" + d.getSeconds();
        // return m_date;
        // 判断时间是否在开盘时间段
        if ((15 * 60 * 60) > (d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds()) && (d.getHours() * 60 * 60 + d.getMinutes() * 60 + d.getSeconds()) > (9 * 60 * 60 + 15 * 60)) {
            return true
        } else {
            return false
        }
    }


    // 获取涨停板数据
    function get_now_limit() {
        var m_data = []
        $.ajax({
            url: '/get_now_limit',
            data: {},
            type: 'POST',
            async: true,
            dataType: 'json',
            success: function(data) {
                m_data = data
                set_table(data['date']['list'])
                set_option()
            },
            error: function(msg) {
                console.log(msg);
            }
        })
        return m_data;
    };

    // 获取炸板数据
    function get_broke() {
        var m_data = []
        $.ajax({
                url: '/limit_broke',
                data: {},
                type: 'POST',
                async: true,
                dataType: 'json',
                success: function(data) {
                    // m_data = data
                    set_table_broke(data['data']['list'])
                },
                error: function(msg) {
                    console.log(msg);
                }
            })
            // return m_data;
    };

    function set_table(data) {
        all_theme = [];
        var m_html = ""
        var m_th = "<tr class=\"cur\">\
            <th>股票名称</th>\
            <th>涨停时间</th>\
            <th>状态</th>\
            <th>涨停原因</th>\
            <th>板块</th>\
            <th>最后涨停时间</th>\
            <th>最大封单</th>\
            <th>封单</th>\
            <th>主力净额</th>\
            <th>成交额</th>\
            <th>实际换手</th>\
            <th>实际流通</th>\
            </tr>"
        var m_list = data
        for (var i_row = 0; i_row < m_list.length; i_row++) {
            if (i_row % 2 == 0) {
                m_html += "<tr>"
            } else
                m_html += "<tr class=\"cur\">"

            // 设置颜色(是否回封)
            if (m_list[i_row][24] == 1) {
                if (m_list[i_row][0].startsWith('3') || m_list[i_row][0].startsWith('68')) {
                    m_html += "<td class=\"gbnews red\">" + m_list[i_row][1] + "</td>" //设置股票名称的颜色，20厘米涨幅是红色
                } else
                    m_html += "<td class=\"gbnews\">" + m_list[i_row][1] + "</td>"
            } else {
                if (m_list[i_row][0].startsWith('3') || m_list[i_row][0].startsWith('68')) {
                    m_html += "<td class=\"red\">" + m_list[i_row][1] + "</td>" //设置股票名称的颜色，20厘米涨幅是红色
                } else
                    m_html += "<td>" + m_list[i_row][1] + "</td>"
            }

            m_html += "<td>" + format_date(new Date(parseInt(m_list[i_row][6]) * 1000)) + "</td>"
            if (m_list[i_row][9] != "首板") {
                m_html += "<td class=\"red\">" + m_list[i_row][9] + "</td>"
            } else
                m_html += "<td>" + m_list[i_row][9] + "</td>"
            m_html += "<td>" + m_list[i_row][16] + "</td>"
                //数据统计
            all_theme = all_theme.concat({
                name: m_list[i_row][16],
                value: 1
            })
            m_html += "<td>" + m_list[i_row][11] + "</td>"
            m_html += "<td>" + format_date(new Date(parseInt(m_list[i_row][25]) * 1000)) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][23]) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][8]) + "</td>"
                // 设置颜色
            if (m_list[i_row][12] > 0) {
                m_html += "<td class=\"red\">" + format_money(m_list[i_row][12]) + "</td>"
            } else
                m_html += "<td class=\"gre\">" + format_money(m_list[i_row][12]) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][13]) + "</td>"
            m_html += "<td>" + m_list[i_row][14] + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][15]) + "</td>"

            m_html += "</tr>"
        }
        var caption = "<caption align=\"top\">涨停-" + m_list.length + "</caption>"
        var tb = document.getElementById("mytable"); //获取表格的dom节点
        tb.innerHTML = caption + m_th + m_html
    }

    function set_table_history(data, data1) {
        all_theme = [];
        var m_html = ""
        var m_th = "<tr class=\"cur\">\
            <th>股票名称</th>\
            <th>涨停时间</th>\
            <th>状态</th>\
            <th>涨停原因</th>\
            <th>板块</th>\
            <th>最后涨停时间</th>\
            <th>最大封单</th>\
            <th>封单</th>\
            <th>主力净额</th>\
            <th>成交额</th>\
            <th>实际换手</th>\
            <th>实际流通</th>\
            <th>开盘涨幅</th>\
            <th>涨幅</th>\
            <th>赚钱效应</th>\
            </tr>"
        var m_list = data
        for (var i_row = 0; i_row < m_list.length; i_row++) {
            if (i_row % 2 == 0) {
                m_html += "<tr>"
            } else
                m_html += "<tr class=\"cur\">"

            // 设置颜色(是否回封)
            if (m_list[i_row][24] == 1) {
                if (m_list[i_row][0].startsWith('3') || m_list[i_row][0].startsWith('68')) {
                    m_html += "<td class=\"gbnews red\">" + m_list[i_row][1] + "</td>" //设置股票名称的颜色，20厘米涨幅是红色
                } else
                    m_html += "<td class=\"gbnews\">" + m_list[i_row][1] + "</td>"
            } else {
                if (m_list[i_row][0].startsWith('3') || m_list[i_row][0].startsWith('68')) {
                    m_html += "<td class=\"red\">" + m_list[i_row][1] + "</td>" //设置股票名称的颜色，20厘米涨幅是红色
                } else
                    m_html += "<td>" + m_list[i_row][1] + "</td>"
            }

            m_html += "<td>" + format_date(new Date(parseInt(m_list[i_row][6]) * 1000)) + "</td>"
            if (m_list[i_row][9] != "首板") {
                m_html += "<td class=\"red\">" + m_list[i_row][9] + "</td>"
            } else
                m_html += "<td>" + m_list[i_row][9] + "</td>"
            m_html += "<td>" + m_list[i_row][16] + "</td>"
                //数据统计
            all_theme = all_theme.concat({
                name: m_list[i_row][16],
                value: 1
            })
            m_html += "<td>" + m_list[i_row][11] + "</td>"
            m_html += "<td>" + format_date(new Date(parseInt(m_list[i_row][25]) * 1000)) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][23]) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][8]) + "</td>"
                // 设置颜色
            if (m_list[i_row][12] > 0) {
                m_html += "<td class=\"red\">" + format_money(m_list[i_row][12]) + "</td>"
            } else
                m_html += "<td class=\"gre\">" + format_money(m_list[i_row][12]) + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][13]) + "</td>"
            m_html += "<td>" + m_list[i_row][14] + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][15]) + "</td>"

            _open = ((parseFloat(data1[m_list[i_row][0]][2]) - parseFloat(data1[m_list[i_row][0]][0])) / parseFloat(data1[m_list[i_row][0]][0]) * 100).toFixed(2) //开盘涨幅
            _close = ((parseFloat(data1[m_list[i_row][0]][1]) - parseFloat(data1[m_list[i_row][0]][0])) / parseFloat(data1[m_list[i_row][0]][0]) * 100).toFixed(2) //收盘涨幅
            raise = _close - _open //实体涨幅
            raising_limit = parseFloat(data1[m_list[i_row][0]][3]).toFixed(2) //涨停价

            //设置字体颜色
            if (_open > 0) {
                //设置涨停背景颜色
                if (parseFloat(data1[m_list[i_row][0]][2]) == parseFloat(data1[m_list[i_row][0]][3])) {
                    m_html += "<td class=\"white rbnews\">" + _open + "</td>"
                } else
                    m_html += "<td class=\"red\">" + _open + "</td>"
            } else if (_open < 0) {
                if (parseFloat(data1[m_list[i_row][0]][2]) == parseFloat(data1[m_list[i_row][0]][4])) {
                    m_html += "<td class=\"white gbnews\">" + _open + "</td>"
                } else
                    m_html += "<td class=\"gre\">" + _open + "</td>"
            } else
                m_html += "<td >" + _open + "</td>"

            if (_close > 0) {
                if (parseFloat(data1[m_list[i_row][0]][1]) == parseFloat(data1[m_list[i_row][0]][3])) {
                    m_html += "<td class=\"white rbnews\">" + _close + "</td>"
                } else
                    m_html += "<td class=\"red\">" + _close + "</td>"
            } else if (_close < 0) {
                if (parseFloat(data1[m_list[i_row][0]][1]) == parseFloat(data1[m_list[i_row][0]][4])) {
                    m_html += "<td class=\"white gbnews\">" + _close + "</td>"
                } else
                    m_html += "<td class=\"gre\">" + _close + "</td>"
            } else
                m_html += "<td>" + _close + "</td>"

            if (raise > 0) {
                m_html += "<td class=\"red\">" + raise.toFixed(2) + "</td>"
            } else if (raise < 0) {
                m_html += "<td class=\"gre\">" + raise.toFixed(2) + "</td>"
            } else
                m_html += "<td >" + raise.toFixed(2) + "</td>"

            m_html += "</tr>"

        }
        var caption = "<caption align=\"top\">涨停-" + m_list.length + "</caption>"
        var tb = document.getElementById("mytable"); //获取表格的dom节点
        tb.innerHTML = caption + m_th + m_html
    }

    function set_table_broke(data) {
        all_theme = [];
        var m_html = ""
        var m_th = "<tr class=\"cur\">\
            <th>股票名称</th>\
            <th>涨幅</th>\
            <th>板块</th>\
            <th>成交额</th>\
            </tr>"
        var m_list = data
        for (var i_row = 0; i_row < m_list.length; i_row++) {
            if (i_row % 2 == 0) {
                m_html += "<tr>"
            } else
                m_html += "<tr class=\"cur\">"

            m_html += "<td>" + m_list[i_row][1] + "</td>"
                // 设置颜色
            if (m_list[i_row][4] > 0) {
                m_html += "<td class=\"red\">" + m_list[i_row][4] + "</td>"
            } else
                m_html += "<td class=\"gre\">" + m_list[i_row][4] + "</td>"
            m_html += "<td>" + m_list[i_row][11] + "</td>"
            m_html += "<td>" + format_money(m_list[i_row][13]) + "</td>"
            m_html += "</tr>"
        }
        var tb = document.getElementById("broke"); //获取表格的dom节点
        var caption = "<caption align=\"top\">炸板-" + m_list.length + "</caption>"
        tb.innerHTML = caption + m_th + m_html
    }

    function format_money(money) {
        //金钱转换
        if (money > 0) {
            if (money / 10000 > 10000) {
                return (money / 100000000).toFixed(2) + "亿"
            } else if (money / 10000 > 1) {
                return (money / 10000).toFixed(0) + "万"
            }
        } else {
            money = Math.abs(money);
            if (money / 10000 > 10000) {
                return "-" + (money / 100000000).toFixed(2) + "亿"
            } else if (money / 10000 > 1) {
                return "-" + (money / 10000).toFixed(0) + "万"
            }
        }
    }

    function format_date(data) {
        //格式化日期 (data=时间戳)
        new Date(parseInt(data) * 1000)
        var h = addZero(data.getHours());
        var m = addZero(data.getMinutes());
        var s = addZero(data.getSeconds());
        return h + ":" + m + ":" + s;

        function addZero(i) {
            if (i < 10) {
                i = "0" + i;
            }
            return i;
        }
    }
</script>

<script type="text/javascript">
    var dom = document.getElementById("container");
    var myChart = echarts.init(dom);
    var app = {};

    var option;

    function set_option() {
        option = {
            tooltip: {
                trigger: 'item'
            },
            series: [{
                name: 'Access From',
                type: 'pie',
                radius: '100%',
                data: count_all(),
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }]
        };

        if (option && typeof option === 'object') {
            myChart.setOption(option);
        }
    }

    function count_all() {
        //数据统计
        var fruitTotal = []; // 存最终数据结果

        // 数据按照水果名称进行归类
        var nameContainer = {}; // 针对键name进行归类的容器
        all_theme.forEach(item => {
            nameContainer[item.name] = nameContainer[item.name] || [];
            nameContainer[item.name].push(item);
        });

        // console.log(nameContainer); // 按照水果名称归类完成：{ apple: Array(2), banana: Array(2) }

        // 统计不同种类水果的数量
        var fruitName = Object.keys(nameContainer); // 获取水果种类：["apple", "banana"]
        fruitName.forEach(nameItem => {
            let count = 0;
            nameContainer[nameItem].forEach(item => {
                count += item.value; // 遍历每种水果中包含的条目计算总数
            });
            fruitTotal.push({
                value: count,
                name: nameItem
            });
        });

        // console.log(fruitTotal);
        return fruitTotal
    }
</script>

</html>